<template>
  <div>
    <div class="login">
      <div class="userinfo">
        <img :src="userInfo.avatar" />
        <span class="nickname">{{ userInfo.nickname }}</span>
        <van-button @click="$router.push('/login')" v-if="flag" type="primary">去登陆</van-button>
        <van-button @click="delToken" v-else type="primary">退出</van-button>
      </div>
    </div>
    <van-grid :column-num="3">
      <van-grid-item @click="$router.push('/favorate')" icon="star-o" text="我的收藏" />
      <van-grid-item @click="$router.push('/rent')" icon="photo-o" text="我的出租" />
      <van-grid-item icon="photo-o" text="看房记录" />
      <van-grid-item icon="photo-o" text="成为房主" />
      <van-grid-item icon="photo-o" text="个人资料" />
      <van-grid-item icon="photo-o" text="联系我们" />
    </van-grid>
    <div class="My_ad__1wZZj">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="" />
    </div>
  </div>
</template>

<script>

import { getUserInfo } from '@/api/user.js'
import { Dialog } from 'vant'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'MY',
  data () {
    return {
      flag: true,
      userInfo: {
        avatar: 'http://liufusong.top:8080/img/profile/avatar.png',
        nickname: '游客'
      }
    }
  },

  created () {
    this.getinfo()
  },
  computed: {
    ...mapState(['userToken'])
  },
  methods: {
    ...mapMutations(['removeToken']),
    async getinfo () {
      if (!this.userToken) return
      try {
        const res = await getUserInfo()
        this.flag = false
        this.userInfo = res.data.body
        this.userInfo.avatar = 'http://liufusong.top:8080' + res.data.body.avatar
      } catch (err) {
        Dialog.alert({
          message: err.message,
          theme: 'round-button'
        })
      }
    },
    delToken () {
      this.flag = true
      this.removeToken()
      this.userInfo = {
        avatar: 'http://liufusong.top:8080/img/profile/avatar.png',
        nickname: '游客'
      }
    }
  }
}
</script>

<style scoped lang="less">
.login {
  min-height: 300px;
  background: url(http://liufusong.top:8080/img/profile/bg.png) no-repeat;
  background-size: contain;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  .userinfo {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    width: 320px;
    height: 165px;
    background-color: #fff;
    img {
      width: 70px;
      height: 70px;
      border-radius: 50%;
      border: 5px solid #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -100%);
    }
    .nickname {
      margin-top: 10px;
      margin-bottom: 20px;
      font-size: 13px;
    }
    .van-button--primary {
      font-size: 13px;
      height: 30px;
      line-height: 30px;
      padding: 0 15px;
      border-radius: 5px;
    }
  }
}
.My_ad__1wZZj {
  margin-top: 20px;
  width: 100%;
  height: 100%;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>
